
package com.welsy.user.command
{
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.welsy.user.business.*;
	import com.welsy.user.vo.*;	
	import com.welsy.authoring.model.CourseModelLocator;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import mx.controls.Alert;
	import mx.collections.ICollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ArrayUtil;	
	import com.welsy.authoring.vo.course.TestComponents;
	import com.welsy.authoring.vo.course.TestVO;
	import com.welsy.authoring.vo.course.Question;
	import com.welsy.authoring.vo.course.Answer;
	import com.welsy.user.event.GetTestTrackEvent;	
	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class GetTestTrackCommand implements ICommand, IResponder
	{
	  	public function GetTestTrackCommand()
		{	 
		}
	
		public function execute( event : CairngormEvent ): void
		{
			var getTestEvent:GetTestTrackEvent = GetTestTrackEvent( event );
			
			var delegate : GetTestTrackDelegate = new GetTestTrackDelegate( this );
			delegate.getTestComponents(getTestEvent.testID,getTestEvent.userId);			    			
		}
	
		public function result( event : Object ) : void
		{				
			var testXML:XMLList = event.result as XMLList;
			var testComponents:TestComponents = new TestComponents;
			testComponents = parseXMLTestComponents(testXML);
			
			var model : CourseModelLocator = CourseModelLocator.getInstance();
			   		
			trace("TEST: " + testComponents.test.title)
			model.selectedTest = testComponents;	
		}
		
		private function parseXMLTestComponents(list:XMLList):TestComponents
		{			
			var tc:TestComponents = new TestComponents;
			var test:TestVO = new TestVO;
			
			//get test properties
			var testXmlRootList:XMLList =  list.child(TestQNames.qnGetTestComponentsResult).children();
			test.setProperties(testXmlRootList)
			
			//set testcomponents obj
			tc.test = test;
			
			//get testcomponents properties					
			var item:XML;	
			var questionsXmlList:XMLList = testXmlRootList.child(TestQNames.qnQuestionComponentsType);				
									
	        for each(item in questionsXmlList) {	            	
            	var questionXmlList:XMLList = item.child(TestQNames.qnQuestion);
      		            		
            	var question:Question = new Question	            		            				            
            	question.setProperties(questionXmlList);
                
                //add answer
                var answerXmlList:XMLList = item.child(TestQNames.qnAnswers).children();
                for each(item in answerXmlList) {
                	            			            		       		            		
            		var answer:Answer = new Answer();	            		            				            
            		answer.setProperties(item);

                	//add answer to the question obj
                	question.addItem(answer); 
		    	}
		    	
		    	//add question to the testcomponents obj
                tc.addItem(question); 	  		            
			}
			
			return tc;
		}
	
		public function fault( event : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( event );
			Alert.show( "Test could not be retrieved!" );
		}
	}

}